package org.yu.team2023.flashdays.client;

import net.minecraft.client.color.item.ItemColor;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TridentItem;
import net.minecraft.world.level.ItemLike;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.*;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;
import org.yu.team2023.flashdays.FlashDays;
import org.yu.team2023.flashdays.TreasureData;
import org.yu.team2023.flashdays.client.render.*;
import org.yu.team2023.flashdays.item.TreasureCurios;
import org.yu.team2023.flashdays.item.UnidentifiedTreasure;

import static net.minecraftforge.client.event.RenderLevelStageEvent.Stage.AFTER_PARTICLES;


@OnlyIn(Dist.CLIENT)
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT, modid = FlashDays.MODID)
public class ClientEventHandle {

    @SubscribeEvent
    public static void onRenderTick(TickEvent.RenderTickEvent event) {
        if (event.phase.equals(TickEvent.Phase.END))
            DamageParticlesManager.tick();
    }

    @SubscribeEvent
    public static void onLevelRender(RenderLevelStageEvent event) {
        if (event.getStage() == AFTER_PARTICLES) {
            DamageParticlesManager.render(event.getPoseStack());
        }
    }


    @Mod.EventBusSubscriber(modid = FlashDays.MODID,value = Dist.CLIENT,bus =Mod.EventBusSubscriber.Bus.MOD)
    public static class ModHandle {

        @SubscribeEvent(priority = EventPriority.HIGHEST)
        public static void onDamageColor(DamageParticleEvent event) {
            if (event.getText().getString().startsWith("msg.")) {
                event.setText(Component.translatable(event.getText().getString()));
            }
        }

        @SubscribeEvent
        public static void onColor(RegisterColorHandlersEvent.Item event){
            event.register((pStack, pTintIndex) -> {
                var data=TreasureData.safeCreate(pStack);
                if(data!=null){
                    if(pTintIndex==0)
                        return data.getQuality().getColor();
                    return 0xFFC125;
                }
                return -1;
            }, ForgeRegistries.ITEMS.getValues().stream().filter(i->i instanceof TreasureCurios).toArray(ItemLike[]::new));
            event.register((pStack, pTintIndex) -> {
                if(pStack.getItem() instanceof UnidentifiedTreasure treasure)
                    return treasure.getQuality().getColor();
                return -1;
            }, ForgeRegistries.ITEMS.getValues().stream().filter(i->i instanceof UnidentifiedTreasure).toArray(ItemLike[]::new));
        }
        @SubscribeEvent
        public static void onRegisterKeyMappings(RegisterKeyMappingsEvent event) {
            ModInputs.ALL_KEY_MAPPINGS.forEach(event::register);
        }
    }
}
